package com.etc.traval.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.etc.traval.dao.SellerDao;
import com.etc.traval.dao.SellercomDao;
import com.etc.traval.dao.UserDao;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.Seller;
import com.etc.travel.entity.Sellercom;

import oracle.jdbc.internal.OracleTypes;

public class SellercomDaoImpl implements SellercomDao{
JDBCUtil util=new JDBCUtil();
UserDao udao=new UserDaoImpl();
SellerDao sdao=new SellerDaoImpl();
	/**
	 * 分页查找评价
	 * @author lwh
	 * @return 所有该商家的评价
	 * @param id:商家的id
	 * @param page:所在页面
	 * @param size:该页面的大小
	 * 
	 */
	@Override
	public List<Sellercom> queryBySellercom(int id,int page,int size) {
		// TODO Auto-generated method stub
		List<Integer> list=new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		List<Sellercom> list1=new ArrayList<Sellercom>();
		Object[] obj=util.callHasReturn("{call proc_queryBySellercom}", list, page,size,id);
		ResultSet rs=(ResultSet) obj[0];
		try {
			while(rs.next()){
				Sellercom se=new Sellercom();
				se.setSellercom_content(rs.getString("sellercom_content"));
				se.setSellercom_id(rs.getInt("sellercom_id"));
				se.setSellercom_time(rs.getTimestamp("sellercom_time"));
				se.setSellercom_user(udao.queryUserById(rs.getInt("sellercom_user")));
				se.setSellercom_seller(sdao.selectSellerById(rs.getInt("sellercom_seller")));
				list1.add(se);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		return list1;
	}
/**
 * 添加商家评论
 * @author lwh
 * @return 是否添加成功 
 * @param 投诉详细信息
 * 
 */
	@Override
	public boolean addSellercom(Sellercom sellercom) {
		// TODO Auto-generated method stub
		return util.update("insert into t_sellercom(sellercom_seller,sellercom_time,sellercom_user,sellercom_content)values(?,?,?,?)",sellercom.getSellercom_seller().getSeller_id(),sellercom.getSellercom_time(),sellercom.getSellercom_user().getUser_id(),sellercom.getSellercom_content())>0;
	}
	/**
	 * 删除商家评论
	 * @author lwh
	 * @return 是否删除成功 
	 * @param 订单id
	 * 
	 */
	@Override
	public boolean delSellercom(int id) {
		// TODO Auto-generated method stub
		return util.update("delete from t_sellercom where sellercom_id=?", id)>0;
	}

	
	
	/**
	 * 根据评论热度分页排序商家
	 * @author cjw
	 * @param page 页数
	 * @param size 每页条数
	 * @return 查询到的数据
	 */
	@Override
	public List<Seller> queryAllSellerByCom(int page, int size) {
		List<Seller> list2 = new ArrayList<Seller>();
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call queryAllSellerByCom(?,?,?)}", list,size,page);
		ResultSet rs = (ResultSet) obj[0];
		SellerDao dao = new SellerDaoImpl();
		try {
			while (rs.next()) {
			Seller seller=dao.selectSellerById(rs.getInt("sellercom_seller"));
			seller.setSellerGoodComNum(rs.getInt("goodnum"));
			ResultSet rs2=util.query("select count(sellercom_seller) as allcom from t_sellercom where sellercom_seller=?", rs.getInt("sellercom_seller"));
			if(rs2.next()){
				seller.setSellerComNum(rs2.getInt("allcom"));
			}
			list2.add(seller);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally {
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e2) {
			}
		}	
		return list2;
		
	}


	
	/**
	 * 分页查找评价
	 * @author lwh
	 * @return 所有该商家的评价
	 * @param id:用户的id
	 * @param page:所在页面
	 * @param size:该页面的大小
	 * 
	 */
	@Override
	public List<Sellercom> queryByUserSellercom(int id, int page, int size) {
		// TODO Auto-generated method stub
				List<Integer> list=new ArrayList<Integer>();
				list.add(OracleTypes.CURSOR);
				List<Sellercom> list1=new ArrayList<Sellercom>();
				Object[] obj=util.callHasReturn("{call proc_querySellercomByUser(?,?,?,?)}",list, page,size,id);
				ResultSet rs=(ResultSet) obj[0];
				try {
					while(rs.next()){
						Sellercom se=new Sellercom();
						se.setSellercom_content(rs.getString("sellercom_content"));
						se.setSellercom_id(rs.getInt("sellercom_id"));
						se.setSellercom_time(rs.getTimestamp("sellercom_time"));
						se.setSellercom_user(udao.queryUserById(rs.getInt("sellercom_user")));
						se.setSellercom_seller(sdao.selectSellerById(rs.getInt("sellercom_seller")));
						list1.add(se);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					try {
						rs.close();
						util.closeAll();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}		
				}
				return list1;
	}
	
	/**查询所有酒店评论总数
	 * @author 叶圣莹
	 * @return num 评论总数
	 */
	@Override
	public int SellerComCount() {
		ResultSet rs= util.query("select count(*) as sellercomnum from t_sellercom ");
		int num=0;
		try {
			if(rs.next())
			num=rs.getInt("sellercomnum");
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			util.closeAll();
		}
		return num;
	}
	
	/**
	 * @author 刘一清
	 * @param seller
	 * @return count
	 * @date 2018年6月7日
	 */
	@Override
	public int queryCountBySeller(Seller seller) {
		int count=0;
		ResultSet rs=util.query("select count(*) as mycount from t_sellercom where sellercom_seller=?", seller.getSeller_id());
		try {
			if(rs.next()){
				count=rs.getInt("mycount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

}
